/*
 * Created by JFormDesigner on Mon May 24 16:36:04 CST 2021
 */

package com.dfrz.bookstore.view;

//import com.dfrz.bookstore.entry.*;
import com.dfrz.bookstore.entry.*;
import com.dfrz.bookstore.service.impl.*;

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class Pay extends JFrame {

    User user = null;
    Book book = null;
    int num = 0;
    List<ShoppingCar> shoppingCars;

    /*购物车购买调用这个*/
    public Pay(User user,List<ShoppingCar> shoppingCars){
        initComponents();
        this.user = user;
        /*获取到购物车 被选中的部分*/
        this.shoppingCars = shoppingCars;
        setTitle(user.getName()+"请选择支付方法");
        Double sum = 0.0;
        for (ShoppingCar shoppingCar : shoppingCars) {
            sum+=shoppingCar.getBookPrice()*shoppingCar.getBookNum();
        }

        String sumStr = new DecimalFormat("######.00").format(sum);
        price.setText(sumStr);

        ImageIcon icon = new ImageIcon(getClass().getResource("/zfb.jpg"));
        icon.setImage(icon.getImage().getScaledInstance(130,130,Image.SCALE_DEFAULT));
        zfbImg.setIcon(icon);
        zfbImg.setBounds(121, 26, 130, 130);
        panel1.add(zfbImg);

        ImageIcon  icon2 = new ImageIcon(getClass().getResource("/wx.jpg"));
        icon2.setImage(icon2.getImage().getScaledInstance(130,130,Image.SCALE_DEFAULT));
        wxImg.setIcon(icon2);
        wxImg.setBounds(121, 26, 130, 130);
        panel2.add(wxImg);


    }
    /*商品详情直接购买调用这个*/
    public Pay(User user, Book book,int num) {
        initComponents();
        this.user = user;
        this.book = book;
        this.num = num;
        setTitle(user.getName()+"请选择支付方法");
        String format = new DecimalFormat("######.00").format(book.getPrice() * book.getDiscount() * num / 10);
        price.setText(format);

        ImageIcon icon = new ImageIcon(getClass().getResource("/zfb.jpg"));
        icon.setImage(icon.getImage().getScaledInstance(130,130,Image.SCALE_DEFAULT));
        zfbImg.setIcon(icon);
        zfbImg.setBounds(121, 26, 130, 130);
        panel1.add(zfbImg);

        ImageIcon  icon2 = new ImageIcon(getClass().getResource("/wx.jpg"));
        icon2.setImage(icon2.getImage().getScaledInstance(130,130,Image.SCALE_DEFAULT));
        wxImg.setIcon(icon2);
        wxImg.setBounds(121, 26, 130, 130);
        panel2.add(wxImg);
    }
    OrderServiceImpl orderService = new OrderServiceImpl();
    ShoppingCarServiceImpl carService = new ShoppingCarServiceImpl();
    OrderItemServiceImpl orderItemService = new OrderItemServiceImpl();
    BookServiceImpl bookService = new BookServiceImpl();
    private void button1ActionPerformed(ActionEvent e) {
        /*直接 支付成功 关闭窗口*/

        /*先创建一个订单 然后获取订单的id  设置订单项的order_id = 订单的id*/
        Order order = new Order();
        order.setUser_id(user.getId());
        order.setOrder_item_id(String.valueOf(new Date().getTime()));//时间蹉
        orderService.inertOrder(order);
        /*这样就有了一个新的订单对象*/
        Order order1 = orderService.findOrderByOrder(order).get(0);
        /*生成对应的订单项即可 简简单单*/
        if (shoppingCars!=null){
            for (int i = 0; i <shoppingCars.size() ; i++) {
                OrderItem orderItem = new OrderItem();
                ShoppingCar shoppingCar = shoppingCars.get(i);
                orderItem.setUser_id(shoppingCar.getUserId());
                orderItem.setBook_id(shoppingCar.getBookId());
                orderItem.setOrder_id(order1.getId());//这里绑定order1.id
                orderItem.setPrice_sum(shoppingCar.getBookPrice()* shoppingCar.getBookNum());
                //调用订单项的servlet插入
                orderItemService.inertOrderItem(orderItem,shoppingCars.get(i).getBookNum());
                //调用ShoppingCarServlet 清空购物车
                carService.delectShoppingCarById(shoppingCars.get(i).getId());
                //给图书减少库存
                Book book = new Book();
                book.setId(shoppingCar.getBookId());
                book = bookService.findBookByBook(book).get(0);
                book.setStore(book.getStore()-shoppingCar.getBookNum());//减少
                bookService.upDataBook(book);//更新数量
            }

        }else if (book!=null){
            OrderItem orderItem = new OrderItem();
            orderItem.setUser_id(user.getId());
            orderItem.setBook_id(book.getId());
            orderItem.setOrder_id(order1.getId()); //这里绑定order1.id
            orderItem.setPrice_sum(book.getPrice() * book.getDiscount() *  num / 10);//计算打折后的价格
            //调用servlet插入即可
            //减少库存
            orderItemService.inertOrderItem(orderItem,num);
            book.setStore(book.getStore()-num);
            bookService.upDataBook(book);
        }
        JOptionPane.showMessageDialog(null, "支付成功");

        dispose();
        new Client(user,new Book()).setVisible(true);


    }

    private void thisWindowClosed(WindowEvent e) {
        Book book1 = new Book();
        book1.setFlag(1);
        new Client(user,book1).setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        tabbedPane1 = new JTabbedPane();
        panel1 = new JPanel();
        zfbImg = new JLabel();
        panel2 = new JPanel();
        wxImg = new JLabel();
        panel3 = new JPanel();
        label5 = new JLabel();
        label6 = new JLabel();
        textField1 = new JTextField();
        label8 = new JLabel();
        panel4 = new JPanel();
        label7 = new JLabel();
        button1 = new JButton();
        label2 = new JLabel();
        price = new JLabel();
        label4 = new JLabel();

        //======== this ========
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                thisWindowClosed(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8bf7\u9009\u62e9\u652f\u4ed8\u65b9\u5f0f");
        label1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(25, 15), label1.getPreferredSize()));

        //======== tabbedPane1 ========
        {

            //======== panel1 ========
            {
                panel1.setLayout(null);
                panel1.add(zfbImg);
                zfbImg.setBounds(145, 35, 130, 130);

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel1.getComponentCount(); i++) {
                        Rectangle bounds = panel1.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel1.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel1.setMinimumSize(preferredSize);
                    panel1.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u652f\u4ed8\u5b9d", panel1);

            //======== panel2 ========
            {
                panel2.setLayout(null);
                panel2.add(wxImg);
                wxImg.setBounds(120, 30, 130, 130);

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel2.getComponentCount(); i++) {
                        Rectangle bounds = panel2.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel2.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel2.setMinimumSize(preferredSize);
                    panel2.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u5fae\u4fe1", panel2);

            //======== panel3 ========
            {
                panel3.setLayout(null);

                //---- label5 ----
                label5.setText("\u4f59\u989d");
                panel3.add(label5);
                label5.setBounds(45, 25, 45, label5.getPreferredSize().height);

                //---- label6 ----
                label6.setText("999");
                panel3.add(label6);
                label6.setBounds(125, 25, 70, label6.getPreferredSize().height);
                panel3.add(textField1);
                textField1.setBounds(120, 90, 160, textField1.getPreferredSize().height);

                //---- label8 ----
                label8.setText("\u652f\u4ed8\u5bc6\u7801:");
                panel3.add(label8);
                label8.setBounds(new Rectangle(new Point(45, 95), label8.getPreferredSize()));

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel3.getComponentCount(); i++) {
                        Rectangle bounds = panel3.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel3.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel3.setMinimumSize(preferredSize);
                    panel3.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u8d26\u6237\u4f59\u989d", panel3);

            //======== panel4 ========
            {
                panel4.setLayout(null);

                //---- label7 ----
                label7.setText("\u672a\u5f00\u901a");
                panel4.add(label7);
                label7.setBounds(55, 25, 125, 105);

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel4.getComponentCount(); i++) {
                        Rectangle bounds = panel4.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel4.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel4.setMinimumSize(preferredSize);
                    panel4.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u94f6\u884c\u5361\\\u4fe1\u7528\u5361", panel4);
        }
        contentPane.add(tabbedPane1);
        tabbedPane1.setBounds(30, 70, 425, 215);

        //---- button1 ----
        button1.setText("\u652f\u4ed8");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 10f));
        button1.addActionListener(e -> button1ActionPerformed(e));
        contentPane.add(button1);
        button1.setBounds(185, 320, 115, 34);

        //---- label2 ----
        label2.setText("\u5f85\u652f\u4ed8");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(210, 20), label2.getPreferredSize()));
        contentPane.add(price);
        price.setBounds(263, 15, 80, 30);

        //---- label4 ----
        label4.setText("\u5143");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(345, 20), label4.getPreferredSize()));

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTabbedPane tabbedPane1;
    private JPanel panel1;
    private JLabel zfbImg;
    private JPanel panel2;
    private JLabel wxImg;
    private JPanel panel3;
    private JLabel label5;
    private JLabel label6;
    private JTextField textField1;
    private JLabel label8;
    private JPanel panel4;
    private JLabel label7;
    private JButton button1;
    private JLabel label2;
    private JLabel price;
    private JLabel label4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
